﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;
using System.Reflection;
using System.Web.Routing;
using MvcEngine.Mvc.Models.ViewModels;

namespace MvcEngine.Mvc.UI
{
    public class FormViewItem : MvcEngineModel
    {
        protected string templateControl;
        protected Type type;
        protected IDictionary<string, object> htmlAttributes = new Dictionary<string, object>();
        private IDictionary<string, object> items = new Dictionary<string, object>();
        protected string name;
        protected string label;
        protected object extraModel;
        protected object itemModel;

        public string TemplateControl
        {
            get
            {
                return templateControl;
            }
        }

        public IDictionary<string, object> Attributes
        {
            get { return htmlAttributes; }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        public string Label
        {
            get
            {
                return label;
            }
        }

        public object ExtraModel
        {
            get
            {
                return extraModel;
            }
        }

        public object ItemModel
        {
            get
            {
                return itemModel;
            }
        }

        public Type Type
        {
            get { return type; }
        }


        public IDictionary<string, object> Items
        {
            get { return items; }
        }
    }

    public class FormViewItem<TItemModel> : FormViewItem
    {
        public FormViewItem(TItemModel itemModel, Type type, string name)
        {
            this.name = name;
            this.type = type;
            this.itemModel = itemModel;
        }

        public FormViewItem<TItemModel> SetName(string name)
        {
            this.name = name;
            return this;
        }

        public FormViewItem<TItemModel> SetLabel(string label)
        {
            this.label = label;
            return this;
        }

        public FormViewItem<TItemModel> SetAttributes(object attributes)
        {
            this.htmlAttributes = ((IDictionary<string, object>)new RouteValueDictionary(attributes));
            return this;
        }

        public FormViewItem<TItemModel> SetTemplateControl(string templateControl)
        {
            this.templateControl = templateControl;
            return this;
        }

        public FormViewItem<TItemModel> SetExtraModel(object extraModel)
        {
            this.extraModel = extraModel;
            return this;
        }

        public FormViewItem<TItemModel> SetExtraValue(string key, object value)
        {
            if (this.Items.ContainsKey(key))
                this.Items[key] = value;
            else
                this.Items.Add(key, value);
            return this;
        }

        public new TItemModel ItemModel
        {
            get
            {
                if (itemModel is TItemModel)
                {
                    return (TItemModel)itemModel;
                }
                return default(TItemModel);
            }
        }
    }
}
